<?php

if(isset($_POST['submit'])){
	
	$search = mysql_real_escape_string($_POST['search']);
	$tags = mysql_real_escape_string($_POST['tags']);
	$page = mysql_real_escape_string($_GET['page']);
	
	$problem = false;
	
	if(empty($search) && empty($tags)){
		$problem = true;
		$content .= '
			<div class="formcontent whole"><P class="error">Please enter in a search and/or tags</P></div>
		';
	}
	
	if(empty($m_username)){
		$problem = true;
		$content .= '
			<div class="formcontent whole"><P class="error">Please login to search for items. Or, <a href="'.$addr.'/register.php">create an account</a>.</P></div>
		';
	}
	
	if(!$problem){
		
		if(!empty($search)){
			$search_array = explode(' ',$search);
			$arr_length = count($search_array);
			
			for($x = 0;$x < $arr_length; $x++){
				$search_array[$x] = trim($search_array[$x]);
			}
		}
		
		if(!empty($tags)){
			$tag_array = explode(',',$tags);
			$arr_length = count($tag_array);
			
			for($x = 0;$x < $arr_length; $x++){
				$tag_array[$x] = trim($tag_array[$x]);
			}
		}
	
		// Matches
		$matches;
	
		/*
		**  Search by:
		**	K E Y W O R D
		*/
		
		// search keywords present, but
		// no tags are entered
		if(!empty($search) && empty($tags)){
			
			$sql = 'SELECT items.item_id FROM items WHERE ';
			
			$search_size = count($search_array);
			
			for($x=0;$x<$search_size;$x++){
				$sql .= "(items.name LIKE '%{$search_array["$x"]}%' OR items.description LIKE '%{$search_array["$x"]}%')";
				if($x != $search_size - 1 && $search_size != 1){
					$sql .= ' AND ';
				}
			}
			
		// search keywords present and
		// tags are present
		}else if(!empty($search) && !empty($tags)){
			
			$tag_size = count($tag_array);
			
			// If only one tag
			if($tag_size == 1){
				$tag_lower_name = strtolower($tag_array[0]);
				
				$query = mysql_query("SELECT tag_id FROM tags WHERE lower_name='$tag_lower_name'");
				$tag_info_array = mysql_fetch_assoc($query);
				$tag_id = $tag_info_array['tag_id'];
				
				$tag_sql = "SELECT tagmap.item_id FROM tagmap,items WHERE tagmap.item_id = items.item_id AND tagmap.tag_id = '$tag_id'";
				
			// More than one tag
			}else{
				$tag_sql = "SELECT items.item_id FROM tagmap,items WHERE tagmap.item_id = items.item_id";
				
				for($x=0;$x<$tag_size;$x++){
					$tag_lower_name = strtolower($tag_array["$x"]);
					
					$query = mysql_query("SELECT tag_id FROM tags WHERE lower_name='$tag_lower_name'");
					$tag_info_array = mysql_fetch_assoc($query);
					$tag_id = $tag_info_array['tag_id'];
					
					// For the first one
					if($x == 0){
						$tag_sql .= " AND (tagmap.tag_id = '$tag_id' OR ";
						
					// For the last one
					}else if($x == ($tag_size - 1)){
						$tag_sql .= "tagmap.tag_id = '$tag_id') GROUP BY items.item_id HAVING COUNT(items.item_id)=$tag_size ";
					
					// For every other one
					}else{
						$tag_sql .= "tagmap.tag_id ='$tag_id' OR ";
					}
				}
			}
			
			$search_size = count($search_array);
			
			$search_sql = 'SELECT items.item_id FROM items WHERE ';
			
			for($x=0;$x<$search_size;$x++){
				$search_sql .= "(items.name LIKE '%{$search_array["$x"]}%' OR items.description LIKE '%{$search_array["$x"]}%')";
				if($x != $search_size - 1 && $search_size != 1){
					$search_sql .= ' AND ';
				}
			}
			
			$sql = "$search_sql AND items.item_id IN($tag_sql)";
			
		// no search keywords, but
		// tags are present
		}else if(empty($search) && !empty($tags)){
			
			$tag_size = count($tag_array);
			
			// If only one tag
			if($tag_size == 1){
				$tag_lower_name = strtolower($tag_array[0]);
				
				$query = mysql_query("SELECT tag_id FROM tags WHERE lower_name='$tag_lower_name'");
				$tag_info_array = mysql_fetch_assoc($query);
				$tag_id = $tag_info_array['tag_id'];
				
				$sql = "SELECT tagmap.item_id FROM tagmap,items WHERE tagmap.item_id = items.item_id AND tagmap.tag_id = '$tag_id'";
				
			// More than one tag
			}else{
				$sql = "SELECT items.item_id FROM tagmap,items WHERE tagmap.item_id = items.item_id";
				
				for($x=0;$x<$tag_size;$x++){
					$tag_lower_name = strtolower($tag_array["$x"]);
					
					$query = mysql_query("SELECT tag_id FROM tags WHERE lower_name='$tag_lower_name'");
					$tag_info_array = mysql_fetch_assoc($query);
					$tag_id = $tag_info_array['tag_id'];
					
					// For the first one
					if($x == 0){
						$sql .= " AND (tagmap.tag_id = '$tag_id' OR ";
						
					// For the last one
					}else if($x == ($tag_size - 1)){
						$sql .= "tagmap.tag_id = '$tag_id') GROUP BY items.item_id HAVING COUNT(items.item_id)=$tag_size ";
					
					// For every other one
					}else{
						$sql .= "tagmap.tag_id ='$tag_id' OR ";
					}
				}
			}
		}
		
		$query = mysql_query($sql) OR die('Mysql query is'.$sql.':<br>'.mysql_error());
		
		/*
		**  D I S P L A Y
		**    M A T C H E S
		*/
		$num_of_items = mysql_num_rows($query);
		if($num_of_items > 0){
			$found  = true;
		}
		
		if($found){
			$page = 1;
			if(empty($_GET['page'])){
				$page = 1;
				$x=0;
			}else{
				$page = $_GET['page'];
				$x = ($page - 1) * 10;
			}
			
			
			while($x < ($page * 10) && $x < $num_of_items){
				$matches = mysql_fetch_assoc($query);
				
				$item_array = mysql_fetch_assoc(mysql_query("SELECT * FROM items WHERE item_id='{$matches["item_id"]}'"));
				
				$content .= '
				<div class="formcontent box"
					onMouseOver="this.style.backgroundColor=\'#CCCCCC\'" 
					onMouseOut="this.style.backgroundColor=\'#FFFFFF\'" 
					'.click_get('item', $item_array['item_id']).'
				>
					<div class="image">
				';
					if(empty($item_array['img_src'])){
						$content .= '<img src="imgs/question_mark.png" alt="There is no image for this item." title="There is no image for this item." />';
					}else{
						$content .= '<img src="images/thumb_'.$item_array['img_src'].'" alt="'.$item_array['img_src'].'" />';
					}
					
				$content .= '
					</div>
					<div class="textboxes">
						<div class="text">
							<div class="label">
							<P>Name: </P>
							</div>
							<div class="data">
							<P>'.nl2br(stripslashes($item_array['name'])).'</P>
							</div>
						</div>
						<div class="text">
							<div class="label">
							<P>Description: </P>
							</div>
							<div class="data">
							<P>'.nl2br(stripslashes($item_array['description'])).'</P>
							</div>
						</div>
					</div>
				</div>
				';
				
				$x++;
			}
			
			if($x < $num_of_items){
				$content .= '
				';
				$content .= '<a href="'.address().this_page().'?page='.++$page.'">Next Page</a>';
			}
		
		// Nothing found
		}else{
			$content .= '
			<P>Sorry. No results were found.</P>
			';
		}
	}
	mysql_close();
}
?>